package org.lanqiao.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.lanqiao.myutil.Myutils;
import org.lanqiao.orm.EleBeforeBill;
import org.lanqiao.orm.EleBeforeCost;
import org.lanqiao.orm.EleMind;
import org.lanqiao.orm.EleOpeartor;
import org.lanqiao.orm.EleUser;
import org.lanqiao.service.INetUserFun;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/netuser")
public class NetUser {

	@Resource
	INetUserFun netUserService;

	@RequestMapping(value = "rechange.action")
	public String rechange() {
		return "user/userRecharge";
	}

	@RequestMapping(value = "netrechange.action")
	public String netRechange(HttpSession session, String start, String end) {
		EleUser user = (EleUser) session.getAttribute("user");
		// 查询是时间段内的账单
		List<EleBeforeBill> allBill = (List) netUserService.userBillList(user.getUserid(), Myutils.normTime(start),
				Myutils.normTime(end));
		session.setAttribute("beforeBill", allBill);
		if (allBill != null) {
			float allBillMoney = 0;
			for (EleBeforeBill bill : allBill) {
				allBillMoney = allBillMoney + bill.getUpmoney();
			}
			session.setAttribute("allBillMoney", allBillMoney);
		}
		return "user/userRecharge";
	}

	@RequestMapping("userNowPay.action")
	public String userNowPay() {
		// 跳转到缴费界面
		return "user/userNowPay";
	}

	@RequestMapping("NowIsPay.action")
	public String userPayMoney(HttpSession session, String inPay) {
		// 缴费
		EleUser user = (EleUser) session.getAttribute("user");
		boolean result = netUserService.nowPay(user, inPay);
		if (result) {
			session.setAttribute("nowIsPay", "缴费成功");
		} else {
			session.setAttribute("nowIsPay", "缴费失败");
		}
		return "user/userNowPay";
	}

	@RequestMapping("modify.action")
	public String userModify() {
		return "user/userInfo";
	}

	@RequestMapping("modifyInfo.action")
	public String userModifyInfo(HttpSession session, EleUser user) {
		// 修改个人信息
		EleUser result = netUserService.modifyInfo(user);
		if (result != null) {
			session.setAttribute("modifyInfo", "更新个人信息成功");
			session.setAttribute("user", result);
		} else {
			session.setAttribute("modifyInfo", "更新个人信息失败");
		}
		return "user/userInfo";
	}

	@RequestMapping(value = "modifyPsw.action")
	public String ModifyPsw() {
		// 修改密码
		return "user/modifyPsw";
	}

	@RequestMapping(value = "usermodifyPaw.action")
	public String userModifyPsw(HttpSession session, String oldPsw, String newPsw) {
		// 进行密码的修改
		EleUser user = (EleUser) session.getAttribute("user");
		boolean result = netUserService.modifyPsw(user, oldPsw, newPsw);
		if (result) {
			session.setAttribute("modifyPsw", "密码修改成功");
		} else {
			session.setAttribute("modifyPsw", "密码修改失败");
		}
		return "user/modifyPsw";
	}

	@RequestMapping(value = "queryCh.action")
	public String queryCh(HttpSession session) {
		// 查询电费
		return "user/userQueryCh";
	}

	@RequestMapping(value = "userQueryCh.action")
	public String userqueryCh(HttpSession session, Date start, Date end) {
		// 查询电费
		List<EleBeforeCost> allEle = netUserService.selectEleCh(start, end);
		if (allEle != null) {
			session.setAttribute("allEle", allEle);
			float allEleMoney = 0;
			float reMoney = 0;
			for (EleBeforeCost ele : allEle) {
				allEleMoney = allEleMoney + ele.getDownmoney();
				if (ele.getCoast() == 0) {
					reMoney += ele.getDownmoney();
				}
			}
			session.setAttribute("allEleMoney", allEleMoney);
			session.setAttribute("reMoney", reMoney);
		}
		return "user/userQueryCh";
	}

	@RequestMapping(value = "helpPerson.action")
	public String helpPerson() {
		// 跳转帮助好友缴费的界面
		return "user/helpPerson";
	}

	@RequestMapping(value = "userHelpPersonSelect.action")
	public String userHelpPerson(HttpSession session, String card) {
		// 查询帮助好友缴费的结果
		EleUser friend = netUserService.helpPersonSelect(card);
		if (friend != null) {
			session.setAttribute("friend", friend);
		}
		return "user/helpPerson";
	}

	@RequestMapping(value = "helpPersonPay.action")
	public String helpPersonPay() {
		// 跳转帮助好友缴费的界面
		return "user/helpPersonPay";
	}

	@RequestMapping(value = "userHelpPersonPay.action")
	public String userHelpPersonPay(HttpSession session, String payMoney) {
		// 帮好友缴费的处理
		EleUser user = (EleUser) session.getAttribute("user");
		EleUser friend = (EleUser) session.getAttribute("friend");
		if (netUserService.helpFriendPay(user, payMoney)) {
			if (netUserService.helpFriend(friend, payMoney)) {
				session.setAttribute("helpFriendResult", "缴费成功");
			} else {
				session.setAttribute("helpFriendResult", "缴费失败");
			}
		}
		return "user/helpPersonPay";
	}

	@RequestMapping(value = "AddSuggest.action")
	public String AddSuggest() {
		// 跳转到提建议界面
		return "user/addSuggest";
	}

	@RequestMapping(value = "userAddSuggest.action")
	public String userAddSuggest(HttpSession session, String suggest) {
		// 提建议
		boolean result = netUserService.addSuggest(suggest);
		if (result) {
			session.setAttribute("addSuggest", "你的建议已经被提交到公司~~");
		} else {
			session.setAttribute("addSuggest", "提交建议失败");
		}
		return "user/addSuggest";
	}

	@RequestMapping(value = "addComplain.action")
	public String addComplain() {
		// 跳转到投诉界面
		return "user/addComplain";
	}

	@RequestMapping(value = "userAddComplain.action")
	public String userAddComplain(HttpSession session, EleMind mind) {
		// 把投诉信存到数据库
		boolean flag = false;
		boolean result = false;
		List<EleOpeartor> allOp = netUserService.selectAllOp();
		String opCard = mind.getOpcode();
		for (EleOpeartor eleOp : allOp) {
			if (eleOp.getOpcode()!=null&&eleOp.getOpcode().equals(opCard)&& opCard != null && !opCard.equals("")) {
				flag = true;
				break;
			}
		}
		if (flag) {
			result = netUserService.addComplain(mind);
			if (result) {
				session.setAttribute("addComplain", "投诉成功");
			}else{
				session.setAttribute("addComplain", "投诉失败");
			}
		}else{
			session.setAttribute("addComplain", "你投诉的人不存在！！");
		}
		
		return "user/addComplain";
	}
}
